#import "DemoDiagram.h"
#import "DiagramCell.h"
#import "DragTester.h"

/* DragTester - This is just a test object to fit between the user interface
and the diagram objects. */

@implementation DragTester

/* Just set up a gray background for the view.  However, in other
situations this could be a more complicated background. */

- appDidInit:sender
{
	NXRect rect;
	[diagram getBounds:&rect];
	diagramBg=[[NXImage alloc] initSize:&rect.size];
	[diagramBg lockFocus];
		PSsetgray(0.5);
		NXRectFill(&rect);
	[diagramBg unlockFocus];
	[diagram setBackground:diagramBg];
	[diagram display];
	return self;
}

/* Creates an image as requested, instantiates a new diagram item, gives
the image to the new item, then adds the item to the diagram. */

- addItem:sender
{
	id image,item;
	NXRect r;
	/* Get an image for the new item */
	NXSetRect(&r,0.0,0.0,(float)[sideInput intValue],(float)[sideInput intValue]);
	image = [[NXImage alloc] initSize:&r.size];
	[self drawImage:image :&r];
	
	/* Get an item and set the image */
	item=[[DiagramCell alloc] init];
	[item setImage:image];
	
	/* Give the new item to the diagram */
	[diagram addItem:item];
	return self;
}

/* Draws the image for the new item as specified at the user interface. */

- drawImage:(NXImage *)image :(NXRect *)r
{
	[image lockFocus];
		[image composite:NX_CLEAR fromRect:r toPoint:&(r->origin)];
		PSsetgray([grayInput floatValue]);
		if ([filledSwitch intValue])
			NXRectFill(r);
		else
			NXFrameRectWithWidth(r,5.0);
	[image unlockFocus];
	return self;
}

@end